﻿using qianjing.APP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;
using thousandClear.Util.WxPay;

using WxPayAPI;

namespace thousandClear.Areas.Shop.Controllers
{
    public class OrderController : BaseController
    {
        // GET: Shop/Order
        public ActionResult Index(string code, string order, string openid, string p)
        {
            OrderModel model = new OrderModel();
            string access_token = "";
            string UserName = "";
            string HeadImage = "";
            try
            {

                AppSetting setting = new AppSetting();
                if (string.IsNullOrEmpty(openid))
                {
                    var i = GetOpenId(code);
                    openid = i.openid;
                    access_token = i.access_token;
                    if (string.IsNullOrEmpty(openid))
                    {
                        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed78d8fdb0030e29&redirect_uri=http%3a%2f%2f{0}%2fshop%2fmain&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect", setting.AppDomainName.Replace("http://", "")));
                    }
                    //更新该用户的微信头像和名称
                    var tt = GetUserInfo(openid, access_token);
                    HeadImage = tt.headimgurl;
                    UserName = tt.nickname;

                    using (var db = new qianjingEntities())
                    {
                        var customer = db.O_Customer.FirstOrDefault(a => a.ocOpenid.Trim() == openid);
                        if (customer != null)
                        {
                            customer.ocNicName = UserName;
                            customer.ocHeadImage = HeadImage;

                            db.SaveChanges();

                        }

                    }
                }

                List<ProjectModel> project = new List<ProjectModel>();

                ViewBag.openid = openid;
                using (var db = new qianjingEntities())
                {
                    //if (!string.IsNullOrEmpty(p))
                    //{
                    //    using (var dbspy = new qianjingEntities())
                    //    {
                    //        var ttemp = dbspy.T_Order.FirstOrDefault(oo => oo.toSerialNo == order);

                    //        string Number = DateTime.Now.ToString("yyMMddHHmmss");
                    //        Random example = new Random();
                    //        int Numradom = example.Next(10000, 99999);
                    //        Number = Number + Numradom.ToString();

                    //        dbspy.T_Order.Add(new T_Order
                    //        {
                    //            toSerialNo = Number,
                    //            toTime = ttemp.toTime,
                    //            toGoodNum = ttemp.toGoodNum,
                    //            toOCID = ttemp.toOCID,
                    //            toTotalRate = ttemp.toTotalRate,
                    //            toOGID = ttemp.toOGID,
                    //            RowStatus = ttemp.RowStatus,
                    //            CreatedDate = ttemp.CreatedDate,
                    //            toItemsGift = ttemp.toItemsGift,
                    //            toOAID = ttemp.toOAID,
                    //            CreatedBy = ttemp.CreatedBy,
                    //            UpdatedBy = ttemp.UpdatedBy,
                    //            toZengSongLieji = ttemp.toZengSongLieji,
                    //            toOATID = ttemp.toOATID,
                    //            toRemark = ttemp.toRemark,
                    //            toSignTime = ttemp.toSignTime
                    //        });
                    //        dbspy.SaveChanges();

                    //        ttemp.RowStatus = 0;
                    //        dbspy.SaveChanges();

                    //        order = Number;
                    //    }
                    //}


                    var record = db.v_Order.FirstOrDefault(a => a.toSerialNo == order);

                    string returnContent = "";
                    if (record.ogsType == 5)
                    {
                        UtilNS.Util.WriteLog("单项开始处理::");
                        string itemText = record.toItemsGift == null ? "" : record.toItemsGift;
                        var tt = itemText.Split(',').ToList();
                        UtilNS.Util.WriteLog("单项开始处理1::"+ itemText);
                        foreach (var yu in tt)
                        {
                            var itu = db.D_item.FirstOrDefault(a => a.DIMID.ToString().Trim() == yu.Trim());
                            returnContent += itu.dimName + ",";
                        }
                        UtilNS.Util.WriteLog("单项开始处理2::" + returnContent);
                    }

                    int time = 1;
                    int signTime = 0;
                    time = Convert.ToInt16(record.ogsSignTime) * Convert.ToInt16(record.ogsTimeMonth) + Convert.ToInt16(record.toSignTime);

                    if (record != null)
                    {

                        if (record.ogsType != 1 && record.ogsType != 5)
                        {
                            if (record.toItemsGift != null)
                            {
                                if (string.IsNullOrEmpty(record.toItemsGift))
                                {
                                    if (record.toItemsGift.Trim().Length > 0)
                                    {
                                        

                                        string[] SprojectItem = record.toItemsGift.Split(',');
                                        signTime = SprojectItem.Length;
                                        foreach (var i in SprojectItem)
                                        {
                                            var desc = db.D_item.FirstOrDefault(a => a.DIMID.ToString() == i);
                                            project.Add(new ProjectModel
                                            {
                                                dimDesc = desc.dimDesc,
                                            });

                                        }
                                    }
                                }
                            }

                        }
                        UtilNS.Util.WriteLog("recordRoro3::" + (record != null).ToString());
                        DateTime _temp = new DateTime();
                        DateTime.TryParseExact(record.osaStartDay, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp);

                        DateTime _temp1 = new DateTime();
                        DateTime.TryParseExact(record.osaEndDay, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp1);

                        int Mouth = Convert.ToInt16(record.ogsSignTime);
                        int yue = int.Parse(record.UpdatedBy == null ? "0" : record.UpdatedBy.ToString());
                        model.time = time;
                        model.a = Convert.ToInt16(string.IsNullOrEmpty(record.CreatedBy) ? "0" : record.CreatedBy);
                        model.ogsSeqNum = Convert.ToInt16(record.ogsSeqNum);
                        model.toTotalRate = record.toTotalRate;
                        model.oasAddressDetail = record.datDesc2 + record.datDesc1 + record.datDesc + record.oasAddressDetail;
                        model.oasName = record.oasName;
                        model.osaPhone = record.osaPhone;
                        model.ogsDesc = record.ogsDesc;
                        model.toTime = Convert.ToInt16(record.toTime);
                        model.toSerialNo = record.toSerialNo;
                        model.CreatedDate = record.CreatedDate;
                        model.toSignTime = Convert.ToInt16(record.toSignTime);
                        model.ocNicName = record.ocNicName;
                        model.toRemark = record.toRemark;
                        model.RowStatus = record.RowStatus;
                        model.type = Convert.ToInt16(record.ogsType);
                        model.osaTimeslot = record.osaTimeslot;
                        model.ogsTimeMonth = Convert.ToInt16(record.ogsTimeMonth);
                        model.startDay = _temp.ToString("yyyy-MM-dd");
                        model.endDay = _temp1.ToString("yyyy-MM-dd");
                        model.ogsTime = (int)record.ogsTime;
                        model.ogsSignTime = (int)record.ogsSignTime;
                        model.ogsTimeMonth = (int)(record.ogsTimeMonth == null ? 1 : record.ogsTimeMonth);
                        model.yue = yue;
                        model.toZengSongLieji = record.toZengSongLieji == null ? 0 : (int)record.toZengSongLieji;
                        model.returnContent = string.IsNullOrEmpty(returnContent) ? "" : returnContent.Substring(0, returnContent.Length - 1);
                        UtilNS.Util.WriteLog("我觉得可以通过:" + returnContent);
                        UtilNS.Util.WriteLog("ogsSeqNum:" + Convert.ToInt16(record.ogsSeqNum));
                    }
                }
                ViewBag.project = project;
            }
            catch (Exception ex)
            {

            }
            return View(model);
        }
        // GET: Shop/CheckOut
        public ActionResult CheckOut(string code, string order, string openId)
        {
            UtilNS.Util.WriteLog(System.DateTime.Now + "order::" + order);
            AddressModel md = new AddressModel();



            md.DATIDdesc = order.Trim();



            order = order.Trim();
            WxPayClient client = new WxPayClient();
            ViewBag.OpenId = openId;
            UtilNS.Util.WriteLog(System.DateTime.Now + "pay openId::" + openId);
            UtilNS.Util.WriteLog(System.DateTime.Now + "pay order::" + order + "++++");

            ViewBag.Vail = "1";
            //校验订单日期是否被延时预约
            using (var db = new qianjingEntities())
            {
                var query = db.v_Order.FirstOrDefault(a => a.toSerialNo == order);
                //var csm = db.O_Goods.FirstOrDefault(a => a.OGSID == query.toOGID);

                string time = query.osaTimeslot;
                string time2 = "0" + time;

                if (query.ogsType == 1)
                {
                    var record = db.v_Order.FirstOrDefault(a => a.toSerialNo != order && (a.osaTimeslot == time || a.osaTimeslot == time2) && a.osaStartDay == query.osaStartDay && a.ogsType == 1 && a.RowStatus != 0 && a.RowStatus != 2 && a.RowStatus != 4);
                    if (record != null)
                    {
                        ViewBag.Vail = "-1";
                    }
                }
                else if (query.ogsType == 7)
                {
                    var record = db.v_Order.Where(a => a.toSerialNo != order && (a.osaTimeslot == time || a.osaTimeslot == time2) && a.osaStartDay == query.osaStartDay && a.ogsType == 7 && a.RowStatus != 0 && a.RowStatus != 2 && a.RowStatus != 4);
                    if (record.Count()>=2)
                    {
                        ViewBag.Vail = "-1";
                    }
                }
                else if (query.ogsType == 5)
                {
                    var record = db.v_Order.FirstOrDefault(a => a.toSerialNo != order && (a.osaTimeslot == time || a.osaTimeslot == time2) && a.osaStartDay == query.osaStartDay && a.ogsType == 5 && a.RowStatus != 0 && a.RowStatus != 2 && a.RowStatus != 4);
                    if (record != null)
                    {
                        ViewBag.Vail = "-1";
                    }
                }
            }

            if (ViewBag.Vail != "-1")
            {
                using (var db = new qianjingEntities())
                {
                    md.DATIDdesc = db.T_Order.FirstOrDefault(a => a.toSerialNo == order).TOID.ToString();
                    var record = db.T_Order.FirstOrDefault(a => a.toSerialNo == order);


                    if (record != null)
                    {

                        var csm = db.O_Customer.FirstOrDefault(a => a.ocOpenid == openId);

                        string Number = DateTime.Now.ToString("yyMMddHHmmss");
                        Random example = new Random();
                        int Numradom = example.Next(10000, 99999);
                        //Number = Number + Numradom.ToString();

                        UnifiedOrderRequest req = new UnifiedOrderRequest();
                        req.Body = "千净";
                        req.Attach = record.TOID.ToString();
                        req.OutTradeNo = record.toSerialNo + "_" + Numradom;
                        ///req.OutTradeNo = Number;
                        req.GoodTag = "good";
                        //req.TotalFee = Convert.ToInt16(record.toTotalRate);
                        req.TotalFee = (int)(record.toTotalRate * 100);
                        req.TradeType = "JSAPI";
                        req.OpenId = openId;
                        req.NotifyUrl = "http://www.qianjing-china.com/thousandClear/admin/service/NotifyUrl";


                        var resp = client.UnifiedOrder(req);

                        UtilNS.Util.WriteLog("pay ErrMsg::" + resp.ErrMsg);

                        WxPayData jsApiParam = new WxPayData();
                        UtilNS.Util.WriteLog(System.DateTime.Now + "appId::" + resp.AppId);
                        jsApiParam.SetValue("appId", resp.AppId);
                        UtilNS.Util.WriteLog(System.DateTime.Now + "timeStamp::" + WxPayApi.GenerateTimeStamp());
                        jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
                        UtilNS.Util.WriteLog(System.DateTime.Now + "nonceStr::" + WxPayApi.GenerateNonceStr());
                        jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
                        UtilNS.Util.WriteLog(System.DateTime.Now + "package::" + resp.PrepayId);
                        jsApiParam.SetValue("package", "prepay_id=" + resp.PrepayId);
                        jsApiParam.SetValue("signType", "MD5");
                        jsApiParam.SetValue("paySign", jsApiParam.MakeSign());

                        ViewBag.AppId = (string)jsApiParam.GetValue("appId");
                        ViewBag.Package = (string)jsApiParam.GetValue("package");
                        ViewBag.NonceStr = (string)jsApiParam.GetValue("nonceStr");
                        ViewBag.Paysign = (string)jsApiParam.GetValue("paySign");
                        ViewBag.TimeStamp = (string)jsApiParam.GetValue("timeStamp");
                        ViewBag.status = 1;
                        if (record.RowStatus == 1)
                        {
                            ViewBag.status = 2;

                        }
                        UtilNS.Util.WriteLog("pay end::");
                    }
                }
            }

            return View(md);

        }
        // GET: Shop/List
        public ActionResult List(string openid, string code, string state, string no)
        {
            AppSetting setting = new AppSetting();
            List<OrderModel> list = new List<OrderModel>();
            try
            {
                ViewBag.no = "0";
                if (!string.IsNullOrEmpty(no))
                {
                    ViewBag.no = no;
                }

                if (no == "6")
                {
                    no = "5";
                }
                UtilNS.Util.WriteLog("no::" + no);
                string access_token = "";
                string UserName = "";
                string headimage = "";
                if (string.IsNullOrEmpty(openid))
                {
                    var i = GetOpenId(code);
                    openid = i.openid;
                    access_token = i.access_token;
                    if (string.IsNullOrEmpty(openid))
                    {
                        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed78d8fdb0030e29&redirect_uri=http%3a%2f%2f{0}%2fshop%2fmain&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect", setting.AppDomainName.Replace("http://", "")));
                        return null;
                    }
                    var tt = GetUserInfo(openid, access_token);
                    headimage = tt.headimgurl;
                    UserName = tt.nickname;
                    UtilNS.Util.WriteLog("UserName::" + UserName);
                    UtilNS.Util.WriteLog("openid::" + openid);
                }

                using (var db = new qianjingEntities())
                {
                    var customer = db.O_Customer.FirstOrDefault(a => a.ocOpenid == openid);
                    if (customer == null)
                    {
                        db.O_Customer.Add(new O_Customer
                        {
                            ocOpenid = openid,
                            ocNicName = UserName,
                            CreatedDate = DateTime.Now,
                            ocHeadImage = headimage,
                            RowStatus = 1,
                            ocGrades = 0,

                        });
                        db.SaveChanges();

                    }

                }
                ViewBag.State = state;
                ViewBag.Code = code;
                ViewBag.Openid = openid;


                using (var db = new qianjingEntities())
                {
                    DateTime _temp = new DateTime();

                    var ocid = db.O_Customer.FirstOrDefault(a => a.ocOpenid == openid).OCID;

                    var query = db.v_Order.AsQueryable();

                    if (!string.IsNullOrEmpty(no))
                    {
                        query = query.Where(a => a.RowStatus.ToString() == no && a.ogsType != 1 && a.ogsType != 5);
                    }
                    var record = query.Where(a => a.toOCID == ocid && a.RowStatus != 2 && a.RowStatus != 0).OrderByDescending(a => a.CreatedDate);

                    UtilNS.Util.WriteLog("record1::" + record);


                    if (record != null)
                    {
                        UtilNS.Util.WriteLog("wo jin lai le");
                        UtilNS.Util.WriteLog(record.Count().ToString());
                        foreach (var item in record)
                        {
                            UtilNS.Util.WriteLog(item.ToString());

                            DateTime.TryParseExact(item.osaStartDay, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp);
                            list.Add(new OrderModel
                            {

                                toTotalRate = item.toTotalRate,
                                oasAddressDetail = item.datDesc2 + item.datDesc1 + item.datDesc + item.oasAddressDetail,
                                oasName = item.oasName,
                                osaPhone = item.osaPhone,
                                ogsDesc = item.ogsDesc,
                                toTime = Convert.ToInt16(item.toTime),
                                toSerialNo = item.toSerialNo,
                                CreatedDate = item.CreatedDate,
                                toSignTime = Convert.ToInt16(item.toSignTime),
                                ocNicName = item.ocNicName,
                                toRemark = item.toRemark,
                                RowStatus = item.RowStatus,
                                type = Convert.ToInt16(item.ogsType),
                                osaTimeslot = item.osaTimeslot,
                                ogsTimeMonth = Convert.ToInt16(item.ogsTimeMonth),
                                startDay = item.osaStartDay,
                                endDay = _temp.AddDays(1).ToString("yyyy-MM-dd"),
                            });



                        }
                    }
                    UtilNS.Util.WriteLog("record2::" + record);
                }
            }
            catch (Exception ex)
            {
                UtilNS.Util.WriteLog("record3::" + ex.Message);
            }

            return View(list);
        }
        // GET: Shop/LookAunt
        public ActionResult LookAunt(string order, string openid)
        {
            AppSetting setting = new AppSetting();
            List<AuntModel> Aunt = new List<AuntModel>();
            List<OrderLogModel> list = new List<OrderLogModel>();
            ViewBag.openid = openid;
            using (var db = new qianjingEntities())
            {
                var csm = db.T_Order.FirstOrDefault(a => a.toSerialNo == order);
                var record = db.T_OrderLog.Where(a => a.tilTOID == csm.TOID).ToList();
                if (record != null)
                {
                    foreach (var item in record)
                    {
                        var projectDesc = db.D_item.FirstOrDefault(a => a.DIMID == item.tilDIID);
                        list.Add(new OrderLogModel
                        {
                            CreatedDate = item.CreatedDate,
                            dimDesc = projectDesc.dimDesc,
                            oatName = projectDesc.dimName,
                        });
                    }
                }
                string auntMessage = csm.toOATID;

                if (!string.IsNullOrEmpty(auntMessage))
                {
                    string[] AuntMessages = auntMessage.Split(',');
                    foreach (var i in AuntMessages)
                    {
                        var aunt = db.O_Aunt.FirstOrDefault(a => a.OATID.ToString() == i);
                        Aunt.Add(new AuntModel
                        {
                            oatName = aunt.oatName,
                            oatNo = aunt.oatNo,
                            ImgUrl = "http://www.qianjing-china.com/thousandClear/Statics/Images/Aunt/" + aunt.oatImg,
                            oatDesc = aunt.oatDesc,
                        });
                    }
                }
                ViewBag.aunt = Aunt;
            }
            ViewBag.order = order;
            return View(list);
        }

        [HttpGet]
        public int change(string id)
        {
            UtilNS.Util.WriteLog(System.DateTime.Now + "changeOn::" + id + "++++++");
            try
            {
                using (var db = new qianjingEntities())
                {

                    var csm = db.T_Order.FirstOrDefault(a => a.TOID.ToString() == id.ToString());
                    var good = db.O_Goods.FirstOrDefault(a => a.OGSID == csm.toOGID);
                    var address = db.O_Address.FirstOrDefault(a => a.OASID == csm.toOAID);
                    var customerGrades = db.O_Customer.FirstOrDefault(a => a.OCID == csm.toOCID);
                    string Phone = address.osaPhone;

                    if (!string.IsNullOrEmpty(csm.toParentID))
                    {
                        using (var tyop = new qianjingEntities())
                        {
                            var oik = tyop.T_Order.FirstOrDefault(a => a.toSerialNo.Trim() == csm.toParentID.Trim());
                            oik.RowStatus = 0;
                            tyop.SaveChanges();
                        }
                    }

                    UtilNS.Util.WriteLog("aaaaatoOGID::" + csm.toOGID + "Rorotesting");

                    UtilNS.Util.WriteLog("aaaaatoOGID::" + (csm.toOGID == 100) + "Rorotesting");

                    string oasNo = "-";
                    if (good.ogsDesc.Trim() == "单次" || good.ogsDesc.Trim() == "单项")
                    {
                        oasNo = "-";
                    }
                    else
                    {
                        if (address.oasNo == null)
                        {
                            oasNo = db.SP_GetRefNo().FirstOrDefault();
                        }
                        else
                        {
                            oasNo = address.oasNo;
                        }
                    }

                    //if (good.ogsType != 5)
                    //{
                    int Signtime = 0;
                    if (good.ogsType == 1)
                    {
                        string msg = "尊敬的客户您好，您已成功预约单次体验保洁服务。客服热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                        SendSms tt = new SendSms();
                        tt.SendMsg(Phone, msg);
                        string Phone1 = "17751289961";
                        string msg1 = "手机号：" + Phone + "的用户已成功预约单次体验保洁服务。订单编号：" + csm.toSerialNo + "。";
                        SendSms ff = new SendSms();
                        ff.SendMsg(Phone1, msg1);

                        SendSms tts2 = new SendSms();
                        tts2.SendMsg("13913587732", msg1);
                        tts2.SendMsg("18912605225", msg1);
                        //tts2.SendMsg("18962134770", msg1);
                    }
                    else if (good.ogsType == 7)
                    {
                        string msg = "尊敬的客户您好，您已成功预订"+ csm.toGoodNum + "小时的服务。服务热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                        SendSms tt = new SendSms();
                        tt.SendMsg(Phone, msg);
                        string Phone1 = "17751289961";
                        string msg1 = "手机号：" + Phone + "的用户已成功预订" + csm.toGoodNum + "小时保洁服务。订单编号：" + csm.toSerialNo + "。";
                        SendSms ff = new SendSms();
                        ff.SendMsg(Phone1, msg1);
                    }
                    else if (csm.toOGID == 100)
                    {
                        string msg = "尊敬的客户您好，您已成功预约单项保洁服务。客服热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                        SendSms tt = new SendSms();
                        tt.SendMsg(Phone, msg);

                        UtilNS.Util.WriteLog("单项服务短信发送给客户::" + Phone);

                        string Phone1 = "17751289961";
                        string msg1 = "手机号：" + Phone + "的用户已成功预约单项保洁服务。订单编号：" + csm.toSerialNo + "。";
                        SendSms ff = new SendSms();
                        ff.SendMsg(Phone1, msg1);

                        SendSms tts2 = new SendSms();
                        tts2.SendMsg("13913587732", msg1);
                        tts2.SendMsg("18912605225", msg1);
                        //tts2.SendMsg("18962134770", msg1);
                    }
                    else
                    {
                        if (good.ogsType == 2)
                        {
                            Signtime = Convert.ToInt16(good.ogsSignTime) - 3;
                        }
                        else if (good.ogsType == 3)
                        {

                            Signtime = Convert.ToInt16(good.ogsSignTime) - 6;

                        }
                        else if (good.ogsType == 4)
                        {
                            Signtime = Convert.ToInt16(good.ogsSignTime) - 12;

                        }
                        else if (good.ogsType == 6)
                        {
                            Signtime = Convert.ToInt16(good.ogsSignTime) - 1;
                        }


                        if (good.ogsTimeMonth > 1)
                        {
                            Signtime = (int)(((good.ogsTime - good.ogsTimeMonth * good.ogsSignTime) / good.ogsTimeMonth) < 0 ? 0 : ((good.ogsTime - good.ogsTimeMonth * good.ogsSignTime) / good.ogsTimeMonth));
                        }

                        if (good.ogsTimeMonth <= 1)
                        {
                            Signtime = 0;
                        }

                        string msg = "尊敬的客户您好，您已成功开通一月" + good.ogsTimeMonth + "次，时长" + good.ogsSignTime + "月，赠送" + Signtime + "月，赠送增时专项" + good.ogsSeqNum + "次的服务。客服热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                        SendSms tt = new SendSms();
                        tt.SendMsg(Phone, msg);
                        string Phone1 = "17751289961";
                        string msg1 = "手机号：" + address.osaPhone + "的用户已成功开通每月" + good.ogsTimeMonth + "次，时长" + good.ogsSignTime + "月，赠送" + Signtime + "月，赠送增时专项" + good.ogsSeqNum + "次的服务。客户编号：" + oasNo + "，订单编号：" + csm.toSerialNo + "。";
                        SendSms ff = new SendSms();
                        ff.SendMsg(Phone1, msg1);

                        SendSms tts2 = new SendSms();
                        tts2.SendMsg("13913587732", msg1);
                        tts2.SendMsg("18912605225", msg1);
                        //tts2.SendMsg("18962134770", msg1);
                    }
                    //}


                    if (!string.IsNullOrEmpty(csm.toParentID))
                    {
                        csm.RowStatus = 5;
                    }
                    else
                    {
                        csm.RowStatus = 1;
                    }

                    csm.UpdatedDate = DateTime.Now;

                    address.oasNo = oasNo;
                    //db.SaveChanges();

                    UtilNS.Util.WriteLog("customerGrades:" + customerGrades.ocGrades);
                    UtilNS.Util.WriteLog("toTotalRate:" + csm.toTotalRate);

                    customerGrades.ocGrades = customerGrades.ocGrades + (int)(csm.toTotalRate * 100 / 10000);

                    UtilNS.Util.WriteLog("customerGrades1:" + customerGrades.ocGrades);
                    db.SaveChanges();


                }
                return 1;
            }
            catch (Exception ex)
            {
                UtilNS.Util.WriteLog(System.DateTime.Now + "changeOff:" + ex.Message.ToString());
                return 1;
            }
        }

        [HttpGet]
        public int refund(long id)
        {
            int resp = 1;
            using (var db = new qianjingEntities())
            {
                var query = db.T_Order.FirstOrDefault(a => a.toSerialNo == id.ToString() && a.RowStatus == 4);
                query.RowStatus = 2;
                query.UpdatedDate = DateTime.Now;
                db.SaveChanges();
                var address = db.O_Address.FirstOrDefault(a => a.OASID == query.toOAID);
                address.RowStatus = 2;
                db.SaveChanges();
            }
            return resp;


        }

        [HttpGet]
        public int stop(long id, string enddate)
        {
            int resp = 1;
            using (var db = new qianjingEntities())
            {
                var query = db.T_Order.FirstOrDefault(a => a.toSerialNo == id.ToString() && a.RowStatus == 5);
                var csm = db.O_Address.FirstOrDefault(a => a.OASID == query.toOAID);
                if (string.IsNullOrEmpty(csm.osaStayTime))
                {
                    DateTime _temp = new DateTime();
                    DateTime _temp1 = new DateTime();
                    DateTime.TryParseExact(csm.osaEndDay, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp);
                    DateTime.TryParseExact(enddate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp1);
                    TimeSpan _temp2 = _temp1 - _temp;
                    query.RowStatus = 3;
                    query.UpdatedDate = DateTime.Now;
                    db.SaveChanges();
                    csm.osaEndDay = enddate;
                    csm.osaStayTime = _temp2.TotalDays.ToString();
                    db.SaveChanges();


                    string Phone = csm.osaPhone;
                    string msg = "尊敬的客户您好，您已成功休眠保洁服务，休眠起始时间" + DateTime.Now + "。客服热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                    SendSms gg = new SendSms();
                    gg.SendMsg(Phone, msg);

                    string Phone1 = "17751289961";
                    string msg1 = "客户编号：" + csm.oasNo + "的用户已成功休眠保洁服务，休眠起始时间" + _temp1 + "日。";
                    SendSms ff = new SendSms();
                    ff.SendMsg(Phone1, msg1);

                    SendSms tts2 = new SendSms();
                    tts2.SendMsg("13913587732", msg1);
                    tts2.SendMsg("18912605225", msg1);
                    //tts2.SendMsg("18962134770", msg1);
                }
                else
                {
                    resp = 2;
                }
            }
            return resp;


        }

        [HttpGet]
        public int go(long id, string enddate)
        {
            int resp = 1;
            using (var db = new qianjingEntities())
            {
                var query = db.T_Order.FirstOrDefault(a => a.toSerialNo == id.ToString() && a.RowStatus == 3);
                var csm = db.O_Address.FirstOrDefault(a => a.OASID == query.toOAID);
                DateTime _temp = new DateTime();
                DateTime.TryParseExact(enddate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out _temp);
                query.RowStatus = 5;
                db.SaveChanges();
                csm.UpdatedDate = DateTime.Parse(_temp.ToString("yyyy-MM-dd hh:mm:ss"));
                db.SaveChanges();
                string Phone = csm.osaPhone;
                string msg = "尊敬的客户您好，您已成功启动保洁服务，启动时间" + DateTime.Now + "。客服热线：400-0808-551；移动热线：177-5128-9961。【千净家政祝您生活愉快】";
                SendSms gg = new SendSms();
                gg.SendMsg(Phone, msg);

                string Phone1 = "177-5128-9961";
                string msg1 = "客户编号：" + csm.oasNo + "的用户已成功启动保洁服务，启动时间" + DateTime.Now + "。";
                SendSms ff = new SendSms();
                ff.SendMsg(Phone1, msg1);
            }
            return resp;


        }
    }
}